package com.zllkj.mm.module.home.fragments;
/*
 * ┌─────────────────────────────────┐
 * │   ┌─┐       ┌─┐                 │
 * │┌──┘ ┴───────┘ ┴──┐              │
 * ││                 │              │
 * ││       ───       │              │
 * ││  ─┬┘       └┬─  │              │
 * ││                 │              │
 * ││       ─┴─       │              │
 * ││                 │              │
 * │└───┐         ┌───┘              │
 * │    │         │                  │
 * │    │         │                  │
 * │    │         │                  │
 * │    │         └──────────────┐   │
 * │    │                        │   │
 * │    │                        ├─┐ │
 * │    │                        ┌─┘ │
 * │    │                        │   │
 * │    └─┐  ┐  ┌───────┬──┐  ┌──┘   │
 * │      │ ─┤ ─┤       │ ─┤ ─┤      │
 * │      └──┴──┘       └──┴──┘      │
 * └─────────────────────────────────┘
 */

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.zllkj.mm.R;
import com.zllkj.mm.api.network.RetrofitHelper;
import com.zllkj.mm.api.network.RxHelper;
import com.zllkj.mm.api.network.subscriber.BaseRxSubscriber;
import com.zllkj.mm.base.BaseAppFragment;
import com.zllkj.mm.common.L;
import com.zllkj.mm.model.entity.DynamicSubscriptionRecordVo;
import com.zllkj.mm.model.entity.DynamicSubscriptionVo;
import com.zllkj.mm.util.common.AppUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Derek.Ren on 2021/8/12.
 * Email:renchun525@163.com
 */
public class DynamicListFragment extends BaseAppFragment implements OnRefreshLoadMoreListener {

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    int pageNum = 1;
    int pageSize = 10;
    boolean isRefresh = true;
    int type = 0;

    List<DynamicSubscriptionRecordVo> dynamicList = new ArrayList<>();
    DynamicListAdapter dynamicListAdapter;


    public static DynamicListFragment newInstance(int type) {
        DynamicListFragment dynamicListFragment = new DynamicListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        dynamicListFragment.setArguments(bundle);
        return dynamicListFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_dynamic_list;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        smartRefreshLayout.setOnRefreshLoadMoreListener(this);
        dynamicListAdapter = new DynamicListAdapter(R.layout.item_square, dynamicList);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(dynamicListAdapter);
        dynamicListAdapter.setEmptyView(R.layout.fragment_empty, (ViewGroup) recyclerView.getParent());
    }

    @Override
    protected void loadData() {
        super.loadData();
        type = getArguments().getInt("type", 0);
        requestData();
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        pageNum++;
        isRefresh = false;
        requestData();
    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        pageNum = 1;
        isRefresh = true;
        requestData();
    }

    void requestData() {
        if (type != 0) {
            addSubscribe(RetrofitHelper.getInstance().getApi()
                    .dynamicNearbyList(new HashMap() {{
                        put("currentPage", pageNum);
                        put("pageSize", pageSize);
                        put("sex", "");
                        put("dynamicType","");
                        put("latitude",-1);
                        put("longitude",-1);
                        put("content","");

                    }})
                    .compose(RxHelper.<DynamicSubscriptionVo>defaultHandleResult())
                    .subscribe(new BaseRxSubscriber<DynamicSubscriptionVo>() {
                        @Override
                        protected void onSucc(DynamicSubscriptionVo o) {
                            if (isRefresh) {
                                smartRefreshLayout.finishRefresh();
                            } else {
                                smartRefreshLayout.finishLoadMore();
                            }
                            if (o.getRecords().size() < pageSize) {
                                L.showToast(pageNum == 1 ? "暂无数据" : "没有更多了");
                                smartRefreshLayout.setNoMoreData(true);
                            }
                            if (isRefresh) {
                                dynamicList.clear();
                            }
                            dynamicList.addAll(o.getRecords());
                            dynamicListAdapter.notifyDataSetChanged();
                        }

                        @Override
                        protected void onFail(String msg) {
                            super.onFail(msg);
                            if (isRefresh) {
                                smartRefreshLayout.finishRefresh();
                            } else {
                                pageNum--;
                                smartRefreshLayout.finishLoadMore();
                            }
                        }
                    })
            );
        } else {
            addSubscribe(RetrofitHelper.getInstance().getApi()
                    .dynamicSubscriptionList(new HashMap() {{
                        put("currentPage", pageNum);
                        put("pageSize", pageSize);
                        put("sex", "");
                        put("dynamicType","");
                        put("latitude", AppUtils.lat);
                        put("longitude",AppUtils.lng);
                        put("content","");

                    }})
                    .compose(RxHelper.<DynamicSubscriptionVo>defaultHandleResult())
                    .subscribe(new BaseRxSubscriber<DynamicSubscriptionVo>() {
                        @Override
                        protected void onSucc(DynamicSubscriptionVo o) {
                            if (isRefresh) {
                                smartRefreshLayout.finishRefresh();
                            } else {
                                smartRefreshLayout.finishLoadMore();
                            }
                            if (o.getRecords().size() < pageSize) {
                                L.showToast(pageNum == 1 ? "暂无数据" : "没有更多了");
                                smartRefreshLayout.setNoMoreData(true);
                            }
                            if (isRefresh) {
                                dynamicList.clear();
                            }
                            dynamicList.addAll(o.getRecords());
                            dynamicListAdapter.notifyDataSetChanged();
                        }

                        @Override
                        protected void onFail(String msg) {
                            super.onFail(msg);
                            if (isRefresh) {
                                smartRefreshLayout.finishRefresh();
                            } else {
                                pageNum--;
                                smartRefreshLayout.finishLoadMore();
                            }
                        }
                    })
            );
        }
    }

    public class DynamicListAdapter extends BaseQuickAdapter<DynamicSubscriptionRecordVo, BaseViewHolder> {


        public DynamicListAdapter(int layoutResId, @Nullable @org.jetbrains.annotations.Nullable List<DynamicSubscriptionRecordVo> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder baseViewHolder, DynamicSubscriptionRecordVo dynamicSubscriptionVo) {
            baseViewHolder.setText(R.id.tvUserName, dynamicSubscriptionVo.getNikeName())
                    .setText(R.id.tvTag1, dynamicSubscriptionVo.getSex() + "•" + dynamicSubscriptionVo.getCity())
                    .setText(R.id.tvTag2, dynamicSubscriptionVo.getCreateTime())
                    .setText(R.id.titleTv, dynamicSubscriptionVo.getContent())
                    //.setText(R.id.greatTv, "" + dynamicSubscriptionVo.getLikeNum())
                    .setText(R.id.commentTv, "" + dynamicSubscriptionVo.getCommentNum());
        }
    }
}
